<?
	$email = $_REQUEST["NewsletterEmail"];

	if (strlen(trim($email)) > 0)
	{
		if ($_REQUEST["formAction"] == "subscribe")
		{
			$alreadyExists = false;
	
			$lines = file('newsletter.txt');
			foreach ($lines as $line_num => $line) 
			{
				if (strtolower(trim($email)) !=  strtolower(trim($line))) continue;
				
				$alreadyExists = true;
				
				break;
			}
			
			if (!$alreadyExists)
			{
				$fp = fopen('newsletter.txt', 'a');
				fwrite($fp, strtolower(trim($email))."\n");
			}
		}
		else if ($_REQUEST["formAction"] == "unsubscribe")
		{
			$fname = "newsletter.txt";
			$fhandle = fopen($fname,"r");
			$content = fread($fhandle,filesize($fname));
			
			$content = str_replace(strtolower(trim($email))."\n", "", $content);
			
			$fhandle = fopen($fname,"w");
			fwrite($fhandle,$content);
			fclose($fhandle);		
		}	
	}
	header("Location: ".$_REQUEST["page"]);
?>